#!/bin/bash

export HELP=0
export NEW=
export BRANCH=
export SYNC_SUBMODULE=0

while [ -n "$1" ]; do
	case "$1" in
		-b) export BRANCH=$2;shift;;
		-n) export BRANCH=$2; NEW=1;shift;;
		-s) export SYNC_SUBMODULE=1;shift;;
		-h|--help) export HELP=1;break;;
	*) break;;
	esac
	shift;
done

[ -z "$BRANCH" -o $HELP -gt 0 ] && {
	cat <<-EOF
Usage: $0 [<change_branch-option>...] <branch name>

change_branch-option:
	-b	<branch>	checkout into a branch
	-n	<branch>	create a new branch
	-s			sync submodule
	-h|--help		display this help
	EOF
	exit 1
}


git checkout ${NEW:+"-b"} $BRANCH 

[ $? -eq 0 -a $SYNC_SUBMODULE -gt 0 ] && git submodule update --init --recursive

